#include "record.h"
#include <QDateTime>
using namespace std;
Record::Record(){
    QDateTime current = QDateTime::currentDateTime();
    QDateTime epoch = QDateTime(QDate(1970,1,1), QTime(1,1,1));
    this->recordID = QString::number(epoch.secsTo(current)).toStdString();
}
Record::Record(int month, int day, int year, int hours, int minutes, string info, string name, string category, double amount){
    QDateTime current = QDateTime::currentDateTime();
    QDateTime epoch = QDateTime(QDate(1970,1,1), QTime(1,1,1));

    this->recordID = QString::number(epoch.secsTo(current)).toStdString();
    this->dateTime.setDate(QDate(year, month, day));
    this->dateTime.setTime(QTime(hours, minutes));
    this->recordName = name;
    this->recordInfo = info;
    this->amount = amount;
    this->category = category;

    this->recordNameID = this->recordID;
    this->recordNameID.append(" ");
    this->recordNameID.append(getname());
}
Record::Record(QDateTime datetime, string info, string name, string category, double amount){
    QDateTime current = QDateTime::currentDateTime();
    QDateTime epoch = QDateTime(QDate(1970,1,1), QTime(1,1,1));

    this->dateTime = datetime;
    this->recordInfo = info;
    this->recordName = name;
    this->category = category;
    this->amount = amount;
    this->recordID = QString::number(epoch.secsTo(current)).toStdString();
    this->recordNameID = this->recordID;
    this->recordNameID.append(" ");
    this->recordNameID.append(name);
}

string Record::getname(){
    return this->recordName;
}
string Record::getinfo(){
    return this->recordInfo;
}
string Record::getnameid(){
    return this->recordNameID;
}

double Record::getamount(){
    return this->amount;
}
string Record::getid(){
    return this->recordID;
}

QString Record::sgetdatetime(){
    return this->dateTime.toString("dd/MM/yyyy hh:mm:ss");
}

QDateTime Record::getdatetime(){
    return this->dateTime;
}
void Record::setid(string id){
    this->recordID = id;
}
void Record::setname(string name){
    this->recordName = name;
}
void Record::setnameid(string nameid){
    this->recordNameID = nameid;
}

void Record::setamount(double amount){
    this->amount = amount;
}
void Record::setdatetime(QDateTime datetime){
    this->dateTime = datetime;
}
void Record::setdatetime(string input){
    // input is type dd.mm.yyyy hh:mm:ss
    char day[3];
    int i = 0;
    day[3] = '\0';
    char month[3];
    month[3] = '\0';
    char year[5];
    year[5] = '\0';
    char hours[3];
    hours[3] = '\0';
    char minutes[3];
    minutes[3] = '\0';
    char seconds[3];
    seconds[3] = '\0';

    for(i = 0; i < 2; i++)
        day[i] = input[i];
    for(i = 0; i < 2; i++)
        month[i] = input[i + 3];
    for(i = 0; i < 4; i++)
        year[i] = input[i + 6];
    for(i = 0; i < 2; i++)
        hours[i] = input[i + 11];
    for(i = 0; i < 2; i++)
        minutes[i] = input[i + 14];
    for(i = 0; i < 2; i++)
        seconds[i] = input[i + 17];


    this->dateTime.setTime(QTime(atoi(hours),atoi(minutes),atoi(seconds)));
    this->dateTime.setDate(QDate(atoi(year), atoi(month), atoi(day)));
}
void Record::setinfo(string info){
    this->recordInfo = info;
}
void Record::operator =(Record copy){
    this->setid(copy.getid());
    this->setamount(copy.getamount());
    this->setdatetime(copy.getdatetime());
    this->setinfo(copy.getinfo());
    this->setcategory(copy.getcategory());
    this->setnameid(copy.getnameid());
    this->setname(copy.getname());
}
void Record::print()
{
    qDebug()<<"record id="<<QString::fromStdString(getnameid());
    qDebug()<<"amount: "<<getamount()
           <<"\ndate: "<<sgetdatetime()
          <<"\ninfo: "<<QString::fromStdString(getinfo())
         <<"\ncategory: "<<QString::fromStdString(getcategory());
}
void Record::set(int month, int day, int year, int hours, int minutes, string info, string name, string category, string id, double amount){
    this->recordID=id;
    this->recordNameID = id;
    this->recordNameID.append(" ");
    this->recordNameID.append(name);
    this->dateTime.setDate(QDate(year, month, day));
    this->dateTime.setTime(QTime(hours, minutes));
    this->recordName = name;
    this->recordInfo = info;
    this->amount = amount;
    this->category = category;
}
void Record::setcategory(string category){
    this->category = category;
}
string Record::getcategory(){
    return this->category;
}
